package com.faithbj.process;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Date;

/**
 * User: xiongyuanwei
 * Date: 12-7-17
 * Time: 上午12:42
 */
public class StartAndEndDateOfTaskTuple implements Externalizable {

    private long taskId;
    private Date startDate;
    private Date endDate;

    public StartAndEndDateOfTaskTuple() {

    }

    public StartAndEndDateOfTaskTuple(long taskId, Date startDate, Date endDate) {
        this.taskId = taskId;
        this.startDate = startDate;
        this.endDate = endDate;
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeLong(taskId);
        if (startDate != null) {
            out.writeBoolean(true);
            out.writeLong(startDate.getTime());
        } else {
            out.writeBoolean(false);
        }
        if (endDate != null) {
            out.writeBoolean(true);
            out.writeLong(endDate.getTime());
        } else {
            out.writeBoolean(false);
        }
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        taskId = in.readLong();
        if (in.readBoolean()) {
            startDate = new Date(in.readLong());
        }
        if (in.readBoolean()) {
            endDate = new Date(in.readLong());
        }
    }

    public boolean isTaskActive() {
        return startDate != null && endDate == null;
    }

    public long getTaskId() {
        return taskId;
    }

    public void setTaskId(long taskId) {
        this.taskId = taskId;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
}
